package com.example.helloworld;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class Capture extends Activity {

	private int mFacing =	1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_capture);
		
		final CameraSurfaceView view =	new CameraSurfaceView(getApplicationContext());
		FrameLayout preview =			(FrameLayout)findViewById(R.id.preview);
		preview.addView(view);
		
		Button button =		(Button)findViewById(R.id.capture);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				view.capture(new Camera.PictureCallback() {
					
					@SuppressLint("NewApi")
					@Override
					public void onPictureTaken(byte[] data, Camera camera) {
						try {
							Bitmap bitmap =		BitmapFactory.decodeByteArray(data, 0, data.length);
							
							ImageView capture =	(ImageView)findViewById(R.id.img_capture);
							
							CameraInfo info =	new Camera.CameraInfo();
							Camera.getCameraInfo(mFacing, info);
							
							int orientation =	info.orientation;
							
							Log.d("getDisplayOrientation", String.format("info.orientation:[%d]", info.orientation));
							
							Bitmap newBitmap =	null;
							if (orientation != 0) {
								Matrix matrix =	new Matrix();
								matrix.postRotate(orientation);
								newBitmap =		Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
							} else {
								newBitmap=		Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
							}
							
							capture.setImageBitmap(newBitmap);
							
						} catch (Exception e) {
							Log.e("SampleCapture", "Failed to insert image", e);
						}
						
					}
				});
			}
		});
		
		Button capture =	(Button)findViewById(R.id.btn_change_camera);
		capture.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				view.setVisibility(View.GONE);
				
				mFacing =	(mFacing == 0 ? 1 : 0);
				
				view.setVisibility(View.VISIBLE);
			}
		});
				
				
	}

	private class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder holder;
		private Camera camera =	null;
		
		public CameraSurfaceView(Context context) {
			super(context);
			
			holder =	getHolder();
			holder.addCallback(this);
			holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		}
		
		@SuppressLint("NewApi")
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			Log.d("surfaceCreated", (camera == null ? "camera is null" : "camera is not null"));
			
			if (camera != null) {
				camera.stopPreview();
				camera.release();
				camera =	null;
			}
			
			Log.d("holder", holder.toString());
			
			camera =	Camera.open(mFacing);
			
			Log.d("surfaceCreated", "camera : " + camera);
			
			try {
				camera.setPreviewDisplay(holder);
			} catch (Exception e) {
				Log.d("CameraSurfaceView", "Failed to set camera preview.", e);
			}
		}

		@SuppressLint("NewApi")
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			Log.d("surfaceChanged", "camera : " + camera.toString());
			
			if (camera == null) {
				camera =	Camera.open(mFacing);
			}
			
			camera.setDisplayOrientation(90);
			camera.startPreview();
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			Log.d("surfaceDestroyed", "camera : " + camera.toString());
			
			camera.stopPreview();
			camera.stopPreview();
			camera.release();
			camera =	null;
			
		}
		
		public boolean capture(Camera.PictureCallback handler) {
			if (camera != null) {
				camera.takePicture(null, null, handler);
				return true;
			} else {
				return false;
			}
		}
		
		
	}
}
